package com.rui.study.demo.httpclient.util;

import com.rui.study.commons.util.FileUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2018-11-15 17:08
 **/
public class PrintUtil {

    public static final int _1KB = 1024;
    public static final String PATH = "d:\\temp\\download";
    public static final String FILENAME = "RESPONSE";

    public static void print(CloseableHttpResponse response) throws IOException {
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            if (entity.getContentLength() > 4 * _1KB) {
                System.out.println("响应结果过大，已保存到" + PATH + File.separator + FILENAME);
                FileUtil.save(entity.getContent(), PATH, FILENAME);
            } else {
                System.out.println(EntityUtils.toString(entity));
            }
        } finally {
            response.close();
        }
    }
}
